package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.InfoTemplateDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.Organization;
import com.jyu.apts.entity.InfoTemplate;
import com.jyu.apts.entity.User;
import com.jyu.apts.entityEnum.InfoTemplateVisibilityEnum;
import com.jyu.apts.service.InfoTemplateService;
import com.jyu.apts.service.UserService;

@RestController
public class OrganizationInfoTemplateController {

	@Autowired
	InfoTemplateService infoTemplateService;
	@Autowired
	UserService userService;
	
	@RequiresPermissions("organizationInfoTemplate_html")
	@RequestMapping("/organizationInfoTemplate_html")
	public ModelAndView organizationInfoTemplate_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/organizationInfoTemplate.html");
		return mv;
	}
	
	@RequiresPermissions("organizationInfoTemplate_html")
	@RequestMapping("/addOrganizationInfoTemplate")
	public ResultDto addInfoTemplate(String name,String[] dataHeads,String description) {
		StringBuilder sb = new StringBuilder("");
		if(dataHeads == null || dataHeads.length == 0) {
			sb.append("#");
		}
		for(String dataHead : dataHeads) {
			sb.append("#");
			sb.append(dataHead);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		InfoTemplate organizationInfoTemplate = new InfoTemplate();
		organizationInfoTemplate.setName(name);
		organizationInfoTemplate.setAuthor(user);
		organizationInfoTemplate.setDataHeads(sb.toString().substring(1));
		organizationInfoTemplate.setDescription(description);
		organizationInfoTemplate.setVisibility(InfoTemplateVisibilityEnum.PRIVATE.getType());
		infoTemplateService.save(organizationInfoTemplate);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("organizationInfoTemplate_html")
	@RequestMapping("/upOrganizationInfoTemplate")
	public ResultDto upOrganizationInfoTemplate(long id,String[] dataHeads,String name,String description) {
		InfoTemplate organizationInfoTemplate = infoTemplateService.getOne(id);
		if(organizationInfoTemplate == null) {
			return new ResultDto("二维码模板不存在!",null,2);
		}
		StringBuilder sb = new StringBuilder("");
		if(dataHeads == null || dataHeads.length == 0) {
			sb.append("#");
		}
		for(String dataHead : dataHeads) {
			sb.append("#");
			sb.append(dataHead);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		if(organizationInfoTemplate.getAuthor().getOrganization().getId()!=organization.getId()) {
			return new ResultDto("非本机构创建的二维码模板",null,2);
		}
		organizationInfoTemplate.setDescription(description);
		organizationInfoTemplate.setName(name);
		organizationInfoTemplate.setDataHeads(sb.toString().substring(1));
		infoTemplateService.save(organizationInfoTemplate);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("organizationInfoTemplate_html")
	@RequestMapping("/delOrganizationInfoTemplate")
	public ResultDto delOrganizationInfoTemplate(String ids) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		List<InfoTemplate> organizationInfoTemplates = infoTemplateService.findAllById(l_ids);
		for(InfoTemplate infoTemplate : organizationInfoTemplates){
			if(infoTemplate.getAuthor().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("包含非本机构创建的二维码模板",null,2);
			}
		}	
		
		infoTemplateService.delete(organizationInfoTemplates);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("organizationInfoTemplate_html")
	@RequestMapping("/getOrganizationInfoTemplatePage")
	public TableDto<InfoTemplateDto> getOrganizationInfoTemplatePage(Integer pageNumber,Integer pageSize,String search) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		Page<InfoTemplate> pageResult = infoTemplateService.getInfoTemplatePage(pageNumber, pageSize, organization.getId(), search);
		List<InfoTemplate> organizationInfoTemplates = pageResult.getContent();
		List<InfoTemplateDto> organizationInfoTemplateDtos = new ArrayList<InfoTemplateDto>();
		organizationInfoTemplates.forEach(organizationInfoTemplate->organizationInfoTemplateDtos.add(new InfoTemplateDto().convert(organizationInfoTemplate)));
		return new TableDto<InfoTemplateDto>(organizationInfoTemplateDtos,pageResult.getTotalElements());
	}
	
	@RequiresAuthentication
	@RequestMapping("/getAllOrganizationInfoTemplate")
	public List<InfoTemplateDto> getAllOrganizationInfoTemplate() {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		List<InfoTemplateDto> organizationInfoTemplateDtos = new ArrayList<InfoTemplateDto>();
		List<InfoTemplate> organizationInfoTemplates = infoTemplateService.findAllByOrganization(organization.getId());
		organizationInfoTemplates.forEach(organizationInfoTemplate->organizationInfoTemplateDtos.add(new InfoTemplateDto().convert(organizationInfoTemplate)));
		return organizationInfoTemplateDtos;
	}
	
}
